package com.shoppingcart.controller;

import com.code.domain.StringUtils;
import com.code.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TestMain {

    public static void main(String[] args){
        List<Student> list = new ArrayList<>();
        //创建3个对象
        Student u1 = new Student();
        u1.setAge(23);
        u1.setName("xiaoming");
        u1.setId(123456);

        Student u2 = new Student();
        u2.setAge(26);
        u2.setName("xiaofang");
        u2.setId(23456);

        Student u3 = new Student();
        u3.setAge(18);
        u3.setName("honghong");
        u3.setId(845113);

        Student u4 = new Student();
        u4.setAge(18);
        u4.setName("honghong2");
        u4.setId(845115);
        //添加对象到集合
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);

        System.out.println("进入方法=======>");
        compare(list);
        printList(list);

    }

    // 遍历方法 stream().filter()
    private static void compare(List<Student> list){
        // 输出名字为 【xiaofang】的用户信息
        List<Student> list1 = list.stream()
                .filter(student -> StringUtils.equals("xiaofang",student.getName()))
                .collect(Collectors.toList());
        System.out.println(" 输出名字为 【xiaofang】的用户信息 = " + list1);

        // 输出年龄在 19 到 25 之间的人员信息
        List<Student> list2 = list.stream()
                .filter(student -> student.getAge() >= 18 && student.getAge() <= 25)
                .collect(Collectors.toList());
        System.out.println(" 输出年龄在 [19 到 25] 之间的人员信息 = " + list2);

        String str = "aaaaddddff"; // StringUtils.endsWith 结尾是否相同
        System.out.println(StringUtils.endsWith(str,"ff")); // true
        System.out.println(StringUtils.startsWith(str,"aa")); // true
    }

    //stream().map()
    private static void printList(List<Student> list){
        List<String> strMap = list.stream().map(Student::getName).collect(Collectors.toList());
        strMap.forEach(s-> System.out.println(s));

        //去除重复 distinct()
        List<Integer> ageList=list.stream().map(Student::getAge).distinct().collect(Collectors.toList());
        //遍历链表
        ageList.forEach(a->System.out.println(a));

    }

}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Student{

    private Integer id;

    private String name;

    private Integer age;
}

